

#ifndef _PPPP_CHANNEL_H_
#define _PPPP_CHANNEL_H_

#include "PPPP_API.h"
#include "define.h"
#include "CircleBuf.h"
#include "VideoPlayer.h"
#include "Adpcm.h"
#include "CgiPacket.h"

#include "clMutex.h"


class CDevice
{
public:
	CDevice(char *DID, char *user, char *pwd);
	virtual ~CDevice();

	//login/logout
	int Login(int id);
	void Logout();
	void ReconnectImmediately();

	//live stream
	int RealPlay(int streamid);
	int StopPlay();


	//audio
	int StartAudio();
	int StopAudio();

	//talk
	int StartTalk();
	int StopTalk();
	int TalkAudioData(char *audioPCM, int len);

	//ptz
	int PTZControl(int command);




	int CameraControl(int param, int value);
	int GetCGI(int cgi);
	int SetSystemParams(int type, char *msg, int len);


private: 
	int _id;
	char szDID[64];
	char szUser[64];
	char szPwd[64];   
	INT32 m_hSessionHandle;
	int PPPP_IndeedRead(UCHAR channel, CHAR *buf, int len);
	void PPPPClose();


private:
	int cgi_livestream(int bstart, int streamid);
	int cgi_get_common(char *cgi);
	CVideoPlayer *m_pVideoPlayer;


private:  
	clMutex m_CloseMutex;

	// private:
	// 	//for playback
	// 	ENUM_VIDEO_MODE m_EnumPlayBackVideoMode;
	// 	int m_bPlayBackFindIFrame;
	// 	CCircleBuf *m_pPlayBackVideoBuf;
	// 	CVideoPlayer *m_pPlayBackVideoPlayer;
	// 	int m_bPlayBackStreamOK;
	// 	int m_bPlaybackStarted;

private:
	int m_bOnline;
	int m_bPlayStreamOK;
	/************************************************************************/
private:
	//cmd thread
	int m_bCommandThreadRuning;
	pthread_t m_CommandThreadID;
	JNIEnv *m_envLocalCmd;
	//CCircleBuf *m_pCommandBuffer; 
	int StartCommandThread();
	static void* CommandThread(void* param);
	void CommandThreadProcess();  
	void ProcessCommand(int cmd, char *pbuf, int len);

	jmethodID m_jmidMsgNotify;
	void MsgNotify(int MsgType, int Param);

	JNIEnv *m_RecvThreadEnvLocal;


	/************************************************************************/
private:
	//video thread
	int m_bDataThreadRuning;
	int StartRealPlayThread();
	static void* RealPlayThread(void* param); 
	void VideoDataProcess();
	void StreamTypeNotify(int stream);
	pthread_t m_DataThreadID;
	JNIEnv *m_DataThreadEnvLocal;
	ENUM_VIDEO_MODE m_EnumVideoMode;
	int m_bFindIFrame;
	CCircleBuf *m_pVideoBuf;

// 	/************************************************************************/
// private:
// 	//Audio thread
// 	void StopAudioPlay();
// 	int m_bAudioPlayThreadRuning;
// 	clMutex m_AudioControlMetux;
// 	int m_bAudioStarted;
// 	pthread_t m_AudioPlayThreadID;
// 
// 	//CCircleBuf *m_pAudioBuf;
// 	JNIEnv *m_AudioThreadEnvLocal;
// 	void StartAudioPlay();
// 
// 	int StartAudioChannel();
// 	static void* AudioThread(void *param);
// 	void AudioProces();
// 	static void* AudioPlayThread(void *param);
// 	void AudioPlayProcess();
// 	pthread_t m_AudioThreadID;
// 	int m_bAudioThreadRuning;
// 
// 
// 	/************************************************************************/
// private:
// 	//talk
// 	int m_bTalkThreadRuning;
// 	static void* TalkThread(void* param); 
// 	void TalkProcess();
// 	int StartPlaybackChannel();
// 	int StartTalkChannel();
// 
// 	int SendTalk(char * pbuf,int len);
// 	pthread_t m_TalkThreadID;
// private:
// 	void ProcessCameraParam(STRU_CAMERA_PARAMS cameraParams);
// 	void ProcessSnapshot(char *pbuf, int len);
 	int AddCommand(void* data, int len);
void ProcessResult(char *pbuf, int len, int type);
// 
 	void StopOtherThread();
// 	//void MainWindowNotify(int MsgType, int Param);
// 	//void PlayWindowNotify(int MsgType, int Param);
// 
// 
 	int SendDecoderControl(char *msg, int len);
 	int SendCameraControl(char *msg, int len);

private:    
	CAdpcm m_pAudioAdpcm;
	CAdpcm m_pTalkAdpcm; 
	int m_bReconnectImmediately ;
	//CCircleBuf *m_pTalkAudioBuf;

	CCgiPacket m_CgiPacket;
};

#endif
